package com.ruoyi.web.controller.system;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

import com.ruoyi.common.config.RuoYiConfig;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.HousekeepingEnclosure;
import com.ruoyi.system.service.IHousekeepingEnclosureService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 【家政人员附件】Controller
 *
 * @author ruoyi
 * @date 2021-06-17
 */
@RestController
@RequestMapping("/system/enclosure")
public class HousekeepingEnclosureController extends BaseController {
    @Autowired
    private IHousekeepingEnclosureService housekeepingEnclosureService;

    /**
     * 查询【家政人员附件】列表
     */
//    @PreAuthorize("@ss.hasPermi('system:enclosure:list')")
    @GetMapping("/list")
    public TableDataInfo list(HousekeepingEnclosure housekeepingEnclosure) {
        startPage();
        List<HousekeepingEnclosure> list = housekeepingEnclosureService.selectHousekeepingEnclosureList(housekeepingEnclosure);
        return getDataTable(list);
    }

    /**
     * 导出【家政人员附件】列表
     */
//    @PreAuthorize("@ss.hasPermi('system:enclosure:export')")
    @Log(title = "【家政人员附件】", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(HousekeepingEnclosure housekeepingEnclosure) {
        List<HousekeepingEnclosure> list = housekeepingEnclosureService.selectHousekeepingEnclosureList(housekeepingEnclosure);
        ExcelUtil<HousekeepingEnclosure> util = new ExcelUtil<HousekeepingEnclosure>(HousekeepingEnclosure.class);
        return util.exportExcel(list, "enclosure");
    }

    /**
     * 获取【家政人员附件】详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:enclosure:query')")
    @GetMapping(value = "/{heId}")
    public AjaxResult getInfo(@PathVariable("heId") Long heId) {
        return AjaxResult.success(housekeepingEnclosureService.selectHousekeepingEnclosureById(heId));
    }

    /**
     * 新增【家政人员附件】
     */
//    @PreAuthorize("@ss.hasPermi('system:enclosure:add')")
    @Log(title = "【家政人员附件】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(MultipartFile file, HousekeepingEnclosure housekeepingEnclosure) {
        if (null != file) {
            try {
                String profile = RuoYiConfig.getProfile();
                byte[] bytes = file.getBytes();
                //文件夹
                Path path2 = Paths.get(profile + "/hId/" + housekeepingEnclosure.gethId() + "/" + housekeepingEnclosure.getType());
                //具体文件
                Path path = Paths.get(profile + "/hId/" + housekeepingEnclosure.gethId() + "/" + housekeepingEnclosure.getType() + "/" + file.getOriginalFilename());
                File file1 = path.toFile();
                //创建文件夹
                if (!file1.exists()) {
                    path2.toFile().mkdirs();
                }
                if (file1.isFile()) {
                    return AjaxResult.error("文件上传失败,文件存在");
                } else {
                    Files.write(path, bytes);//创建文件
                    housekeepingEnclosure.setUrl("profile/hId/" + housekeepingEnclosure.gethId() + "/" + housekeepingEnclosure.getType() + "/" + file.getOriginalFilename());
                }
            } catch (Exception e) {

            }
        }
        return toAjax(housekeepingEnclosureService.insertHousekeepingEnclosure(housekeepingEnclosure));
    }

    /**
     * 修改【家政人员附件】
     */
//    @PreAuthorize("@ss.hasPermi('system:enclosure:edit')")
    @Log(title = "【家政人员附件】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(MultipartFile file, HousekeepingEnclosure housekeepingEnclosure) {
        if (null != file) {
            try {
                String profile = RuoYiConfig.getProfile();
                byte[] bytes = file.getBytes();
                //文件夹
                Path path2 = Paths.get(profile + "/hId/" + housekeepingEnclosure.gethId() + "/" + housekeepingEnclosure.getType());
                //具体文件
                Path path = Paths.get(profile + "/hId/" + housekeepingEnclosure.gethId() + "/" + housekeepingEnclosure.getType() + "/" + file.getOriginalFilename());
                File file1 = path.toFile();
                //创建文件夹
                if (!file1.exists()) {
                    path2.toFile().mkdirs();
                }
                if (file1.isFile()) {
                    file1.delete();
                } else {
                    Files.write(path, bytes);//创建文件
                    housekeepingEnclosure.setUrl("profile/hId/" + housekeepingEnclosure.gethId() + "/" + housekeepingEnclosure.getType() + "/" + file.getOriginalFilename());
                }
            } catch (Exception e) {

            }
        }
        return toAjax(housekeepingEnclosureService.updateHousekeepingEnclosure(housekeepingEnclosure));
    }

    /**
     * 删除【家政人员附件】
     */
//    @PreAuthorize("@ss.hasPermi('system:enclosure:remove')")
    @Log(title = "【家政人员附件】", businessType = BusinessType.DELETE)
    @DeleteMapping("/{heIds}")
    public AjaxResult remove(@PathVariable Long[] heIds) {
        return toAjax(housekeepingEnclosureService.deleteHousekeepingEnclosureByIds(heIds));
    }
}
